/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JP_Permisos_Mantenimiento.java
 *
 * Created on 14-jun-2014, 16:34:53
 */
package pck_gui;

import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import pck_pojos.Cls_TipoPermiso;
import pck_utilitarios.Cls_Funciones;
import pck_utilitarios.Cls_InvocaWebService;
import pck_utilitarios.Cls_Parametros;
import pck_utilitarios.JP_Modelo;
import pck_utilitarios.Utiles_Panel;
import udprrhh.UDPRRHHView;

/**
 *
 * @author AngelAndres
 */
public class JP_Permisos_Mantenimiento extends JP_Modelo {

    /** Creates new form JP_Permisos_Mantenimiento */
    public JP_Permisos_Mantenimiento() {
        initComponents();
        btnCancelar.setEnabled(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        TXT_CODIGO = new javax.swing.JTextField();
        TXT_DESCRIPCION = new javax.swing.JTextField();
        btnBuscar = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        btnNuevo = new javax.swing.JButton();
        btnGuardar = new javax.swing.JButton();
        btnModificar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        btnEliminar = new javax.swing.JButton();

        setBorder(javax.swing.BorderFactory.createTitledBorder("Mantenimiento de Permisos"));

        jLabel1.setText("Codigo");
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText("Descripción");
        jLabel2.setName("jLabel2"); // NOI18N

        TXT_CODIGO.setEnabled(false);
        TXT_CODIGO.setName("TXT_CODIGO"); // NOI18N

        TXT_DESCRIPCION.setEnabled(false);
        TXT_DESCRIPCION.setName("TXT_DESCRIPCION"); // NOI18N
        TXT_DESCRIPCION.addInputMethodListener(new java.awt.event.InputMethodListener() {
            public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
                TXT_DESCRIPCIONCaretPositionChanged(evt);
            }
            public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
                TXT_DESCRIPCIONInputMethodTextChanged(evt);
            }
        });
        TXT_DESCRIPCION.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                TXT_DESCRIPCIONKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                TXT_DESCRIPCIONKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                TXT_DESCRIPCIONKeyTyped(evt);
            }
        });

        btnBuscar.setText("Buscar");
        btnBuscar.setName("btnBuscar"); // NOI18N
        btnBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Opciones"));
        jPanel1.setName("jPanel1"); // NOI18N

        btnNuevo.setText("Nuevo");
        btnNuevo.setName("btnNuevo"); // NOI18N
        btnNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNuevoActionPerformed(evt);
            }
        });

        btnGuardar.setText("Guardar");
        btnGuardar.setEnabled(false);
        btnGuardar.setName("btnGuardar"); // NOI18N
        btnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuardarActionPerformed(evt);
            }
        });

        btnModificar.setText("Modificar");
        btnModificar.setEnabled(false);
        btnModificar.setName("btnModificar"); // NOI18N
        btnModificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnModificarActionPerformed(evt);
            }
        });

        btnCancelar.setText("Cancelar");
        btnCancelar.setName("btnCancelar"); // NOI18N
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        btnEliminar.setText("Eliminar");
        btnEliminar.setEnabled(false);
        btnEliminar.setName("btnEliminar"); // NOI18N
        btnEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(12, 12, 12)
                .addComponent(btnNuevo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnGuardar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 17, Short.MAX_VALUE)
                .addComponent(btnModificar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnEliminar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(8, 8, 8))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(27, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnGuardar)
                    .addComponent(btnNuevo)
                    .addComponent(btnCancelar)
                    .addComponent(btnEliminar)
                    .addComponent(btnModificar))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(TXT_CODIGO, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(btnBuscar))
                            .addComponent(TXT_DESCRIPCION, javax.swing.GroupLayout.PREFERRED_SIZE, 253, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(TXT_CODIGO, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnBuscar))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(TXT_DESCRIPCION, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNuevoActionPerformed
        TXT_DESCRIPCION.setEnabled(true);
        btnNuevo.setEnabled(false);
        btnGuardar.setEnabled(true);
        btnModificar.setEnabled(false);
        btnCancelar.setEnabled(true);
        btnEliminar.setEnabled(false);
        opcion = "Nuevo";
        //  TXT_DESCRIPCION.setEnabled(false);
        btnBuscar.setEnabled(false);
        //limpiar();
        TXT_DESCRIPCION.setText("");
        TXT_CODIGO.setText("");

    }//GEN-LAST:event_btnNuevoActionPerformed
    public void obtenerPermiso(Cls_TipoPermiso tipoPermiso) {
        TXT_CODIGO.setText(tipoPermiso.getId() + "");
        TXT_DESCRIPCION.setText(tipoPermiso.getDescripcion());
        btnModificar.setEnabled(true);
        btnEliminar.setEnabled(true);
        TXT_DESCRIPCION.setEnabled(false);
        btnCancelar.setEnabled(true);
    }
    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        limpiar();
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuardarActionPerformed
        try {
            if (TXT_DESCRIPCION.getText().length() > 0) {
                if (opcion.equals("Nuevo")) {
                    String parametrosParaService_01[] = {Cls_Parametros.Map_DESCRIPCION_PERMISO, TXT_DESCRIPCION.getText()};
                    System.out.println(TXT_DESCRIPCION.getText());
                    JOptionPane.showMessageDialog(this, Cls_InvocaWebService.nuevoPermiso(Cls_Funciones.generarHashMapParaSolicitud(parametrosParaService_01)));
                } else if (opcion.equals("Modificar")) {
                    String parametrosParaService_01[] = {Cls_Parametros.Map_ID_PERMISO, TXT_CODIGO.getText()};
                    String parametrosParaService_02[] = {Cls_Parametros.Map_DESCRIPCION_PERMISO, TXT_DESCRIPCION.getText()};

                    JOptionPane.showMessageDialog(this, Cls_InvocaWebService.modificarPermiso(Cls_Funciones.generarHashMapParaSolicitud(parametrosParaService_01, parametrosParaService_02)));
                }
                limpiar();
            } else {
                JOptionPane.showMessageDialog(this, "POR FAVOR COMPLETE TODOS LOS CAMPOS");
            }
        } catch (Exception e) {
            //System.out.println(e.getMessage());
        }

    }//GEN-LAST:event_btnGuardarActionPerformed

    private void btnModificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnModificarActionPerformed
        TXT_DESCRIPCION.setEnabled(true);
        btnNuevo.setEnabled(false);
        btnEliminar.setEnabled(false);
        btnGuardar.setEnabled(true);
        btnModificar.setEnabled(false);
        opcion = "Modificar";
    }//GEN-LAST:event_btnModificarActionPerformed

    private void btnBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarActionPerformed
        JInternalFrame jif_permisos_Buscar;
        JP_Permisos_Buscar panel = new JP_Permisos_Buscar();
        jif_permisos_Buscar = Utiles_Panel.creaVentanaInternaModal((UDPRRHHView) this.getRef(), panel, "Busca de Permisos");
        panel.setPadre(Utiles_Panel.TIPO_JINTERNALFRAME, jif_permisos_Buscar);
        panel.setRefDep(this);
    }//GEN-LAST:event_btnBuscarActionPerformed

    private void TXT_DESCRIPCIONKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_TXT_DESCRIPCIONKeyPressed
        //  TXT_DESCRIPCION.setText(remove(TXT_DESCRIPCION.getText()).toUpperCase());
    }//GEN-LAST:event_TXT_DESCRIPCIONKeyPressed

    private void TXT_DESCRIPCIONKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_TXT_DESCRIPCIONKeyReleased
        TXT_DESCRIPCION.setText(TXT_DESCRIPCION.getText().toUpperCase());
    }//GEN-LAST:event_TXT_DESCRIPCIONKeyReleased

    private void TXT_DESCRIPCIONKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_TXT_DESCRIPCIONKeyTyped
        char c = evt.getKeyChar();
        if (c != 32) {
            if (c < 'A' || c > 'Z') {
                c = (char) (c - 32);
            }
            if (c < 'A' || c > 'Z') {
                evt.consume();
                TXT_DESCRIPCION.requestFocus();
            }
            TXT_DESCRIPCION.setText(TXT_DESCRIPCION.getText().toUpperCase());
        }
    }//GEN-LAST:event_TXT_DESCRIPCIONKeyTyped

    private void TXT_DESCRIPCIONInputMethodTextChanged(java.awt.event.InputMethodEvent evt) {//GEN-FIRST:event_TXT_DESCRIPCIONInputMethodTextChanged
        //   TXT_DESCRIPCION.setText(remove(TXT_DESCRIPCION.getText()).toUpperCase());
    }//GEN-LAST:event_TXT_DESCRIPCIONInputMethodTextChanged

    private void TXT_DESCRIPCIONCaretPositionChanged(java.awt.event.InputMethodEvent evt) {//GEN-FIRST:event_TXT_DESCRIPCIONCaretPositionChanged
        //   TXT_DESCRIPCION.setText(remove(TXT_DESCRIPCION.getText()).toUpperCase());
    }//GEN-LAST:event_TXT_DESCRIPCIONCaretPositionChanged

    private void btnEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarActionPerformed
        if (!Cls_Funciones.estaVacio(TXT_CODIGO, TXT_DESCRIPCION)) {
            int opt = JOptionPane.showConfirmDialog(this, "¿DESEA ELIMINAR EL PERMISO?",
                    "CONFIRMACIÓN", JOptionPane.YES_NO_OPTION);
            String[] parametrosParaService1 = {Cls_Parametros.Map_ID_PERMISO,
                TXT_CODIGO.getText()};
            switch (opt) {
                case JOptionPane.YES_OPTION:
                    JOptionPane.showMessageDialog(this, Cls_InvocaWebService.eliminarPermiso(Cls_Funciones.generarHashMapParaSolicitud(parametrosParaService1)));
                    limpiar();
                    break;
                case JOptionPane.NO_OPTION:
                    JOptionPane.showMessageDialog(this, "SE CANCELÓ OPERACIÓN",
                            "AVISO", JOptionPane.INFORMATION_MESSAGE);
                    break;
                default:
                    JOptionPane.showMessageDialog(this, "SE CANCELÓ OPERACIÓN");
            }

        } else {
            JOptionPane.showMessageDialog(this, "NO HAY PERMISO QUE ELMINIAR",
                    "¡Error!", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_btnEliminarActionPerformed
//    private  String remove(String input) {
//    // Cadena de caracteres original a sustituir.
//    String original = "áàäéèëíìïóòöúùuñÁÀÄÉÈËÍÌÏÓÒÖÚÙÜÑçÇ";
//    // Cadena de caracteres ASCII que reemplazarán los originales.
//    String ascii = "aaaeeeiiiooouuunAAAEEEIIIOOOUUUNcC";
//    String output = input;
//    for (int i=0; i<original.length(); i++) {
//        // Reemplazamos los caracteres especiales.
//        output = output.replace(original.charAt(i), ascii.charAt(i));
//    }//for i
//    return output;
//    }

    private void limpiar() {
        TXT_DESCRIPCION.setText("");
        TXT_DESCRIPCION.setEnabled(false);
        TXT_CODIGO.setText("");
        btnGuardar.setEnabled(false);
        btnModificar.setEnabled(false);
        btnNuevo.setEnabled(true);
        opcion = "";
        btnBuscar.setEnabled(true);
        btnCancelar.setEnabled(false);
        btnEliminar.setEnabled(false);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField TXT_CODIGO;
    private javax.swing.JTextField TXT_DESCRIPCION;
    private javax.swing.JButton btnBuscar;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnEliminar;
    private javax.swing.JButton btnGuardar;
    private javax.swing.JButton btnModificar;
    private javax.swing.JButton btnNuevo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables
    private String opcion;
}
